/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotEmpty;

import com.eelpo.common.util.BlankUtils;

/**
 * <pre>
 * 企业表，用于映射T_ENTERPRISE表(Persist Object)
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Entity
@Cacheable
@Table(name = "T_ENTERPRISE")
public class TEnterprise implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 8153603691762880342L;

	/**
	 * 企业Id
	 */
	@NotEmpty
	private String id;

	/**
	 * 企业名称
	 */
	@NotEmpty
	private String name;

	/**
	 * 联系人
	 */
	@NotEmpty
	private String contact;

	/**
	 * 联系电话
	 */
	@NotEmpty
	private String phone;

	/**
	 * 企业状态
	 */
	@NotNull
	private Byte status;

	/**
	 * 企业的序列
	 */
	@NotNull
	private Integer index;

	/**
	 * 企业描述信息
	 */
	private String description;

	/**
	 * 父企业
	 */
	private TEnterprise parent;

	/**
	 * 上级企业的关系列表
	 */
	private Set<TEnterpriseRelation> parentRelation = new HashSet<TEnterpriseRelation>();

	/**
	 * 下级企业的关系列表
	 */
	private Set<TEnterpriseRelation> childRelation = new HashSet<TEnterpriseRelation>();

	public TEnterprise() {

	}

	public TEnterprise(String id) {
		this.id = id;
	}

	@Id
	@Column(length = 36)
	@GenericGenerator(name = "uuid", strategy = "uuid2")
	@GeneratedValue(generator = "uuid")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(nullable = false, length = 64, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(nullable = false, length = 64)
	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	@Column(nullable = false, length = 32)
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(nullable = false)
	public Byte getStatus() {
		return status;
	}

	public void setStatus(Byte status) {
		this.status = status;
	}

	@Column(name = "idx", nullable = false)
	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	@Column(length = 512)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parentId", nullable = true)
	public TEnterprise getParent() {
		return parent;
	}

	public void setParent(TEnterprise parent) {
		this.parent = parent;
	}

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "childEnterprise")
	public Set<TEnterpriseRelation> getParentRelation() {
		return parentRelation;
	}

	public void setParentRelation(Set<TEnterpriseRelation> parentRelation) {
		this.parentRelation = parentRelation;
	}

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parentEnterprise")
	public Set<TEnterpriseRelation> getChildRelation() {
		return childRelation;
	}

	public void setChildRelation(Set<TEnterpriseRelation> childRelation) {
		this.childRelation = childRelation;
	}

	@Override
	public boolean equals(Object obj) {
		if (BlankUtils.isBlank(obj) || !(obj instanceof TEnterprise))
			return false;

		if (this == obj)
			return true;

		TEnterprise enterprise = (TEnterprise) obj;

		if (BlankUtils.isBlank(this.id) || BlankUtils.isBlank(enterprise.getId())) {
			return false;
		}

		return this.id.equals(enterprise.getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return "TEnterprise[name=" + name + "]";
	}
}
